package com.atguigu.gulimail.order.conf;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119607549";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key =  "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC69Jx0BZ5+PVa49A9wJRfj7JS7rKlXjOBAn7ufkZ5RQCs2p9ln8VDYajvE2zUClGmTCGLcMIDutNwWiYXjrpDUqaocI436UH/uOTcUT6N2lXpyGP2QxjS0K5Hi67KaRPI8Dg+92mEZjVvtaMau/Ii49lk8W8YNZeotHQtj77O8MlMMfcwcnf0ZabXrOI1Wq7LYbXRk1tvB/tT6FD1VrgbhT2W7WL8TTlSTWsHU2m5uLiAxfg72K34KwcWbJ0gTMX5UDW4aPdzkDpLzvG7KlfW6KOfvXOEgBawJJ2+GZ7hc+jvGntetFVQ3s6FnGx4DHKta2bEkae+/g8j2g61LJm8TAgMBAAECggEBAKbtSdR+P+nPsuMpZ69mL1KfJp37qKg3bPPGq6vqRUWe2fbPH9sjKGBTWlKGt2o6QZm63ypjVHvOmMS2vglxEHgRlYXSaS6bOd7mzzsfcICtLss5bQ4bK2vDi1rlwG7mKLNeiseleRVrli0AZIZRjXI446x09P15hgIJ8OWmXQEnEwKAwNxpxeIc4JrvDF/ma7SNeUuOSfuFBbijD4LgoZ5L9GavX/yMCUb4tzrqEwPZ/LrTiEBtHM6Uo4AaECLHNEdkH5aCS3EYWBB8URvcJQIgzVZD1K3WYtDM+bjuZOBFl5xhxby72GCo1Vf77wxvtCM9kXsHY3rTlkHcxzxI/uECgYEA/WP+5+GOPZx3VdnBejJhrcPWI13n76Q0JaNFo5mtGZQEPPeIhHAulXrylEDUEUr/ykVF6kGOd0c9iwvkNPAZu1ACzyNmcvyKEX0Vlk+yycnhVm8I0KxJfbFZelXa7/cmHH8l89Rh/L5x0Uv3SW/0Jx8PfMK6XxV86SWyP9SGbXcCgYEAvOF5nAXRCYvhF2TIHjBDmWkbwxWQjvDx5VXS2i0wjvMKtt3fASJc2MEBez4Yv1GRWF59ZJQuTUDFgp6IP8rGmnDMjgNhXwE+JxeuK8JigviZ9VKLa3WqA4H/J8E8NQ7T7iHKLvF9ixzI1Iehlf67qs6aZJKTWE5eXfIWvP5wAkUCgYAYtcKq7DUJzxNYHOcgOn+KCXn9cu/mT7YCIZzt6lOWFfgf1efLOoFgdF/pkzTbBtxQ5lZ+FKyG9zyKLbuwaeWofZH4jzQzbGsub2tGgDkoMna2Dhfm+ZYlGsFmC69RDWH7D28tCv4PPqcsyOKxl6hQlFfXS8oO8bJ/rfxm+xus2wKBgQCCIVYHiHpceWK7G1REeDwala/q2BCgaqA9wMNkTX4G/quDEZlyzpnpPBApOL8UEL5yg6cghHbiAp4RP+ffC3WJMe28adfL/K81f7sfquz0DxMDUbt8bbSUdhu06AzzASRCDEWf0gEMtugmhMtu6/Gf3HfCkrmxPU0GrKO90g6ZgQKBgG/LM1dIdXf4abZjpowWitbZvQI7akRCd79N2MVUFvvi2kLKPaBkzLeP87AEzajh/yS1VS2IlBH3uCdObuQGM4jRyp01v+iFUnqbN/7K4LLckaj4TdBEA2kGEIsuwpQXWqnKcsi4N1pkLpAn+G3NHBLa7r8XZp8jTdgzk+rKoy0A";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsoa1uiCmznyuKzQ2GzK8qL/lAjIKNh+7xWXpD4JAuZrTmcvtCq1YsPZoM+qvC8NSbwteb5yU5Ll54kWhXOa8dl8ZYH1NUq6vNCrujw1h6lYzTEiO+zu+KEpu2ysexhbq1V9v00dSdriOuCry4lKTMu5dKla36MV9I7LndurIXtD2pToTqjfA/y36oIoXtEgjLJzKlgDYkimLPRYjy3Xky00pu3/HUG6c5wx6aw2i9mVCOkDn3Uo4q0HX8cHxtxJJhH/xECFYAvbX+gz5s+zKlrZwcUXEydml+192ibKSbbym4BYBRGw5x3JIWcwoCjK0qsV+z+VPr/qEMLPsh6Y0UwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
